// vite.config.ts
import { loadEnv, defineConfig } from "vite";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import vue from "@vitejs/plugin-vue";
import path from "path";
import viteCompression from "vite-plugin-compression";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
import vueJsx from "@vitejs/plugin-vue-jsx";

const CWD = process.cwd();
// env
const BASE_ENV_CONFIG = loadEnv("", CWD);

// https://vitejs.dev/config/#conditional-config

export default defineConfig(() => {
  return {
    plugins: [
      vue(),
      vueJsx(),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(CWD, "src/assets/icons")],
        // Specify symbolId format
        symbolId: "icon-[dir]-[name]"
      }),
      viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: "gzip",
        ext: ".gz"
      }),
      Components({
        dts: true,
        resolvers: [AntDesignVueResolver()],
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/]
      })
    ],
    resolve: {
      alias: {
        // @/xxxx => src/xxxx
        "@": path.resolve(__dirname, "src"),
        // #/xxxx => types/xxxx
        "#": path.resolve(__dirname, "types")
      }
    },
    server: {
      open: true,
      port: 3000,
      proxy: {
        [BASE_ENV_CONFIG.VITE_API_PROXY_URL]: {
          target: BASE_ENV_CONFIG.VITE_API_ONLINE_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, "")
        }
      }
    }
  };
});
